﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/jquery.formatCurrency-1.4.0.js" />
///<reference path="../../scripts/jquery.formatCurrency.all.js" />


var SearchedProducts = [];
var Culture = "";

function RenderProucts(data) {
    var container = $("#search-products-container:eq(0)");
    container.empty();

    SearchedProducts = data;

    var even = true;
    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        even = !even;
        var row = $("<tr>"
                                + "<td><input type='checkbox' /><input type='hidden' class='search-product-index' />"
                                + "<td><input type='text' class='quantity' value='1' /></td>"
                                + "<td></td>"
                                + "<td></td>"
                                + "<td class='numeral'></td>"

                            + "</tr>");

        if (even === true) {
            row.addClass("even");
        }

        row.find(".search-product-index").val(i);

        row.find("td:eq(2)").html(item.ProductName);

        var names = [];
        var values = [];
        for (var n in item.Variations) {

            var variation = item.Variations[n];
            names.push(variation.Name);
            values.push(variation.Value);

        }

        row.find(".variation-names").val(names.join("-"));
        row.find("td:eq(3)").html(values.join("-"));
        row.find(".numeral").html(item.UnitPrice).formatCurrency({ region: Culture });  //format curreny

        container.append(row);
    }

    bindCheckbox();
}


var OrderItems = [];

function AddOrderItem(item) {
    var isNew = true;
    for (var i = 0; i < OrderItems.length; i++) {
        var find = OrderItems[i];
        if (find.SaleItemId == item.SaleItemId) {
            find.Quantity += item.Quantity;
            find.Total = find.UnitPrice * find.Quantity;

            if (find.Quantity < 1) {
                OrderItems.splice(i, 1);
            }
            isNew = false;
            break;
        }
    }

    //not found
    if (isNew) {
        if (item.Quantity > 0) {
            item.Total = item.UnitPrice * item.Quantity;
            OrderItems.push(item);
        }
    }

    RenderOrderItems();
}

function RenderOrderItems() {
    var container = $("#order-item-container");
    var datacontainer = $("#order-item-data-container");

    datacontainer.empty();
    container.empty();

    if (OrderItems.length > 0) {
        //$("#next_to_order").removeClass("hide");
        $("#next_to_order").removeAttr("disabled");
    } else {
        //$("#next_to_order").addClass("hide");
    $("#next_to_order").attr("disabled", "disabled");
    }

    var even = true;
    for (var i = 0; i < OrderItems.length; i++) {
        var item = OrderItems[i];

        if (item.Quantity > 0) {

            var data = $("<input type=hidden class=index name=OrderItems.index />"
                    + "<input type=hidden class=Id  value=-1 />"
                    + "<input type=hidden class=ProductId  />"
                    + "<input type=hidden class=SaleItemId  />"
                    + "<input type=hidden class=Quantity />"
                    + "<input type=hidden class=UnitPrice  />"
                    + "<input type=hidden class=ProductName />"
                    + "<input type=hidden class=SKU />");

            var name = "OrderItems[" + i + "]";

            data.filter(".index").val(i);
            data.filter(".Id").attr("name", name + ".Id");
            data.filter(".ProductId").attr("name", name + ".ProductId").val(item.ProductId);
            data.filter(".SaleItemId").attr("name", name + ".SaleItemId").val(item.SaleItemId);
            data.filter(".Quantity").attr("name", name + ".Quantity").val(item.Quantity);
            data.filter(".UnitPrice").attr("name", name + ".UnitPrice").val(item.UnitPrice);
            data.filter(".ProductName").attr("name", name + ".ProductName").val(item.ProductName);
            data.filter(".SKU").attr("name", name + ".SKU").val(item.SKU);

            datacontainer.append(data);

            even = !even;

            var row = $("<tr>"
                        + "<td><input type='checkbox' /></td>"
                        + "<td></td>"//Product Name
                        + "<td></td>"//Variation
                        + "<td></td>"//Quantity
                        + "<td></td>"//Total
                        + "</tr>");

            if (even === true) {
                row.addClass("even");
            }

            row.find("td:eq(1)").html(item.ProductName);

            var variations = [];

            for (var n = 0; n < item.OrderItemVariations.length; n++) {
                var variation = item.OrderItemVariations[n];
                variations.push(variation.Value);

                var template = $("<input type=hidden class=index >"
                       + "<input type=hidden class=Id value=-1 >   "
                       + "<input type=hidden class=Name > "
                       + "<input type=hidden class=Value >");

                var index = "OrderItems[" + i + "].OrderItemVariations.index";
                var name = "OrderItems[" + i + "].OrderItemVariations[" + n + "]";

                template.filter(".index").attr("name", index).val(n);
                template.filter(".Id").attr("name", name + ".Id");
                template.filter(".Name").attr("name", name + ".Name").val(variation.Name);
                template.filter(".Value").attr("name", name + ".Value").val(variation.Value);

                datacontainer.append(template);

            }

            for (var n = 0; n < item.OrderItemCustomFields.length; n++) {
                var field = item.OrderItemCustomFields[n];

                var template = $("<input type=hidden class=index >"
                       + "<input type=hidden class=Id value=-1 >   "
                       + "<input type=hidden class=Name > "
                       + "<input type=hidden class=Value >");

                var index = "OrderItems[" + i + "].OrderItemCustomFields.index";
                var name = "OrderItems[" + i + "].OrderItemCustomFields[" + n + "]";

                template.filter(".index").attr("name", index).val(n);
                template.filter(".Id").attr("name", name + ".Id");
                template.filter(".Name").attr("name", name + ".Name").val(field.Name);
                template.filter(".Value").attr("name", name + ".Value").val(field.Value);

                datacontainer.append(template);
            }

            row.find("td:eq(2)").html(variations.join("-"));
            row.find("td:eq(3)").html(item.Quantity);
            row.find("td:eq(4)").html(item.Total).formatCurrency({ region: Culture }); //format currency

            container.append(row);



        }

    }

    EnableCalculator();
}



$(function () {
    $(".product-delete").click(function (e) {

        e.preventDefault();

        $("#order-item-container").find("tr").each(function (index, item) {

            if ($(item).find("td > input:checked").size() > 0) {
                OrderItems[index] = null;
            }
        });

        var items = [];
        for (var i in OrderItems) {
            var item = OrderItems[i];
            if (item != null) {
                items.push(item);
            }
        }

        OrderItems = items;

        RenderOrderItems();

        return false;
    });

    $("#search-products-form").submit(function (e) {
        e.preventDefault();
        $(this).ajaxGet({
            success: function (data) {
                Culture = data.Culture;
                RenderProucts(data.Data);
            }
        });

        return false;
    });

    var firstSearchProduct = false;


    $("#product-select-link").click(function (e) {
        e.preventDefault();

        $("#product-select").dialog("open");
        if (firstSearchProduct == false) {
            $("#search-products-form").submit();
            firstSearchProduct = true;
        }
    });

    $(".search-products-add").click(function (e) {
        e.preventDefault();
        $("#search-products-container > tr").each(function (index, row) {
            var item = $(row);
            if (item.find("input[type=checkbox]").attr("checked") === true) {
                var orderItem = {};
                var find = SearchedProducts[index];
                orderItem.SaleItemId = find.SaleItemId;
                orderItem.ProductId = find.ProductId;
                orderItem.ProductName = find.ProductName;
                orderItem.UnitPrice = find.UnitPrice;
                orderItem.Quantity = parseInt(item.find(".quantity").val());
                orderItem.SKU = find.SKU;
                orderItem.OrderItemCustomFields = find.CustomFields;
                orderItem.OrderItemVariations = find.Variations;
                AddOrderItem(orderItem);
            }
        });

        $("#product-select").dialog("close");

        return false;
    });

    //bind check_all 
    $("#select_all").bind("change", function () {
        var checked = $(this).attr("checked");
        $("#search-products-container :checkbox").each(function (index, obj) {
            $(obj).attr("checked", checked);
        });
    });
});

//bind each checkbox
function bindCheckbox() {
    $("#search-products-container :checkbox").each(function (index, obj) {
        $(obj).bind("change", function () {
            $("#select_all").attr("checked", false);
        });
    });
}